package com.heima.article.listen;

import com.heima.common.constants.article.HotArticleConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 用于热点文章实时计算
 * @author mrchen
 * @date 2022/7/16 11:45
 */
@Component
@Slf4j
public class HotArticleScoreListener {
    @Autowired
    StringRedisTemplate redisTemplate;
    @RabbitListener(queuesToDeclare = @Queue(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE))
    public void listenMsg(String behaviorDto){
        log.info("成功接收到  实时产生的文章行为消息 ==> {}",behaviorDto);
        // [ {"type":VIEWS,"articleId","add": 1},{"type":VIEWS,"articleId","add": 1},{"type":VIEWS,"articleId","add": 1},{"type":VIEWS,"articleId","add": 1}]
        redisTemplate.opsForList().rightPush(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_LIST,behaviorDto);
    }
}
